package com.example.word_book.service;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.word_book.R;
import com.example.word_book.bean.ErrorWord;
import com.example.word_book.bean.Word;
import com.example.word_book.utils.SqlHelp;

import java.util.Random;

public class DictationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dictation);


        init();
    }

    private void init() {
        SqlHelp sqlHelp = new SqlHelp(this);
        Word word = sqlHelp.selectWord(sqlHelp.randomWord());
        ImageView back = findViewById(R.id.img_back);

        EditText edt_word = findViewById(R.id.edt_word);
        edt_word.setText("");

        TextView tv_type = findViewById(R.id.tv_type);
        TextView tv_translation = findViewById(R.id.tv_translation);

        if (word.getType().length == 1) {
            tv_type.setText(word.getType()[0]);
            tv_translation.setText(word.getTranslation()[0]);
        } else {
            int s = new Random().nextInt(word.getType().length);
            tv_type.setText(word.getType()[s]);
            tv_translation.setText(word.getTranslation()[s]);
        }


        TextView tv_word = findViewById(R.id.tv_word);
        tv_word.setText(word.getWord());

        TextView tv_judgment = findViewById(R.id.tv_judgment);
        tv_judgment.setTextColor(getColor(R.color.tomato));

        tv_word.setVisibility(View.GONE);
        tv_judgment.setVisibility(View.GONE);

        Button btn_vague = findViewById(R.id.btn_vague);

        Button btn_confirm = findViewById(R.id.btn_confirm);
        btn_confirm.setText("确定");

        btn_vague.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (edt_word.getText().toString().trim().equals(word.getWord())) {

                    tv_judgment.setText("√");
                } else
                    tv_judgment.setText("×");
                tv_word.setVisibility(View.VISIBLE);
                tv_judgment.setVisibility(View.VISIBLE);
                btn_confirm.setText("下一题");
                sqlHelp.insertErrorWord(new ErrorWord(word.getWord()));
            }
        });


        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (btn_confirm.getText().toString().equals("确定")) {
                    if (edt_word.getText().toString().trim().equals(word.getWord())) {
                        tv_judgment.setTextColor(getColor(R.color.teal_200));
                        tv_judgment.setText("√");
                    } else {
                        tv_judgment.setText("×");
                        sqlHelp.insertErrorWord(new ErrorWord(word.getWord()));
                    }
                    tv_word.setVisibility(View.VISIBLE);
                    tv_judgment.setVisibility(View.VISIBLE);
                    btn_confirm.setText("下一题");
                } else {
                    init();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}